//
//  ListenView.m
//  Kugou-OC
//
//  Created by BH on 16/1/22.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "ListenView.h"
#import "GlobalDefine.h"
#import "RightArrowButton.h"
#import "ScrollLabel.h"
#import "PlayHelper.h"
#import "BottomPlayView.h"
#import <AVFoundation/AVFoundation.h>
#import <SDAutoLayout/UIView+SDAutoLayout.h>

#define Margin 10

@interface ListenView ()

@end

@implementation ListenView
#pragma mark - 懒加载
-(NSArray *)localMusicList{
    if (!_localMusicList) {
        self.localMusicList = [[NSArray alloc] init];
    }
    
    return _localMusicList;
}

#pragma mark - 初始化
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //获取本地音乐
        self.localMusicList = [[PlayHelper sharePlayHelper] getLocalMusic];
        //添加透明背景
        [self addAlphaBgviews];
    }
    return self;
}

/**
 *  添加透明背景
 */
-(void)addAlphaBgviews{
    UIView *bgView1 = [[UIView alloc] init];
    bgView1.layer.masksToBounds = NO;
    bgView1.layer.cornerRadius = 3;
    bgView1.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView2 = [[UIView alloc] init];
    bgView2.layer.masksToBounds = NO;
    bgView2.layer.cornerRadius = 3;
    bgView2.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView3 = [[UIView alloc] init];
    bgView3.layer.masksToBounds = NO;
    bgView3.layer.cornerRadius = 3;
    bgView3.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView4 = [[UIView alloc] init];
    bgView4.layer.masksToBounds = NO;
    bgView4.layer.cornerRadius = 3;
    bgView4.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView5 = [[UIView alloc] init];
    bgView5.layer.masksToBounds = NO;
    bgView5.layer.cornerRadius = 3;
    bgView5.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    UIView *bgView6 = [[UIView alloc] init];
    bgView6.layer.masksToBounds = NO;
    bgView6.layer.cornerRadius = 3;
    bgView6.backgroundColor = COLOR(255, 255, 255, 0.6);
    
    [self sd_addSubviews:@[bgView1,bgView2,bgView3,bgView4,bgView5,bgView6]];
    
    bgView1.sd_layout.leftSpaceToView(self,Margin).topSpaceToView(self,Margin).rightSpaceToView(self,Margin);
    
    bgView2.sd_layout.leftEqualToView(bgView1).topSpaceToView(bgView1,Margin/2).rightEqualToView(bgView1);
    
    bgView3.sd_layout.leftEqualToView(bgView2).topSpaceToView(bgView2,Margin/2).widthRatioToView(self,0.63);
    UITapGestureRecognizer *bgview3Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMusicLibrary:)];
    [bgView3 addGestureRecognizer:bgview3Tap];
    
    bgView4.sd_layout.leftSpaceToView(bgView3,Margin/2).topEqualToView(bgView3).rightEqualToView(bgView2);
    
    bgView5.sd_layout.leftEqualToView(bgView3).topSpaceToView(bgView3,Margin/2).rightEqualToView(bgView4);
    
    bgView6.sd_layout.leftEqualToView(bgView5).rightEqualToView(bgView5).topSpaceToView(bgView5,Margin/2);
    
    //添加subviews
    [self addBgView1Subviews:bgView1];
    [self addBgView2Subviews:bgView2];
    [self addBgView3Subviews:bgView3];
    [self addBgView4Subviews:bgView4];
    [self addBgView5Subviews:bgView5];
    [self addBgView6Subviews:bgView6];
}

-(void)addBgView1Subviews:(UIView *)view{
    UIImageView *iconImgV = [[UIImageView alloc] init];
    iconImgV.image = IMAGE(@"listen_songRecognise_light");
    
    UIImageView *leftLine = [[UIImageView alloc] init];
    leftLine.image = IMAGE(@"line_short_bold_vertical");
    
    UIImageView *searchIcon = ALLOC(UIImageView);
    searchIcon.image = IMAGE(@"listen_search_gray");
    
    UITextField *searchTF = ALLOC(UITextField);
    searchTF.placeholder = @"My demons";
    searchTF.clearsOnBeginEditing = YES;
    searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [view sd_addSubviews:@[iconImgV,leftLine,searchTF,searchIcon]];
    
    iconImgV.sd_layout.leftSpaceToView(view,Margin).topSpaceToView(view,8).widthIs(30).heightIs(30);
    
    leftLine.sd_layout.leftSpaceToView(iconImgV,Margin).topEqualToView(iconImgV).bottomEqualToView(iconImgV).widthIs(1);
    
    searchIcon.sd_layout.rightSpaceToView(view,Margin/2).topEqualToView(iconImgV).widthIs(30).heightEqualToWidth();
    
    searchTF.sd_layout.leftSpaceToView(leftLine,Margin).topEqualToView(view).bottomEqualToView(view).rightSpaceToView(searchIcon,Margin/2);
    
    [view setupAutoHeightWithBottomView:iconImgV bottomMargin:8];
}

-(void)addBgView2Subviews:(UIView *)view{
    
    UIView *vw = [[UIView alloc] init];
    [view addSubview:vw];
    vw.sd_layout.leftEqualToView(view).rightEqualToView(view).topEqualToView(view);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showLocalList:)];
    [vw addGestureRecognizer:tap];
    
    UILabel *localMusicLab = [[UILabel alloc] init];
    localMusicLab.font = TITLE_ONE_FONT;
    localMusicLab.text = @"本地音乐";
    
    RightArrowButton *localMusicCountBtn = [RightArrowButton buttonWithType:UIButtonTypeCustom];
    localMusicCountBtn.titleLabel.font = TITLE_FOUR_FONT;
    [localMusicCountBtn setTitleColor:TITLE_SECOND_TEXT_COLOR forState:UIControlStateNormal];
    [localMusicCountBtn setImage:IMAGE(@"home_cell_arrow_left") forState:UIControlStateNormal];
    [localMusicCountBtn setTitle:[NSString stringWithFormat:@"%d首",(short)_localMusicList.count] forState:UIControlStateNormal];
    
    UIImageView *playImg = [[UIImageView alloc] init];
    playImg.image = IMAGE(@"listen_play_normal");
    
    UIImageView *line = [[UIImageView alloc] init];
    line.image = IMAGE(@"listen_horizon_long_line");
    [vw sd_addSubviews:@[localMusicLab,localMusicCountBtn,playImg,line]];
    
    playImg.sd_layout.rightSpaceToView(vw,20).topSpaceToView(vw,5).widthIs(50).heightEqualToWidth();
    localMusicLab.sd_layout.leftSpaceToView(vw,20).centerYEqualToView(vw).autoHeightRatio(0).widthIs(80);
    localMusicCountBtn.sd_layout.leftSpaceToView(localMusicLab,5).bottomEqualToView(localMusicLab).heightIs(20).widthIs(50);
    line.sd_layout.leftEqualToView(localMusicLab).rightEqualToView(playImg).heightIs(1).topSpaceToView(playImg,5);
    [vw setupAutoHeightWithBottomView:line bottomMargin:0];
    
    NSArray *btnImgs = @[@"listen_myfavorite_light",@"listen_mycollection_normal",@"listen_downloadManage_normal",@"listen_playrecently_normal"];
    NSArray *btnTitles = @[@"我喜欢",@"我的歌单",@"下载管理",@"最近播放"];
    UIButton *btn = nil;
    UILabel *lab = nil;
    for (int i=0; i<btnImgs.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:IMAGE(btnImgs[i]) forState:UIControlStateNormal];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = btnTitles[i];
        label.font = TITLE_THIRD_FONT;
        label.textAlignment = NSTextAlignmentCenter;
        
        [view sd_addSubviews:@[button,label]];
        
        if (btn || label) {
            button.sd_layout.leftSpaceToView(btn,0).topSpaceToView(line,0).heightIs(60).widthRatioToView(view,0.25);
            label.sd_layout.leftSpaceToView(lab,0).topSpaceToView(button,0).widthRatioToView(view,0.25).autoHeightRatio(0);

        }else{
            button.sd_layout.leftEqualToView(view).topEqualToView(line).heightIs(60).widthRatioToView(view,0.25);
            label.sd_layout.leftEqualToView(view).topSpaceToView(button,0).autoHeightRatio(0).widthRatioToView(view,0.25);
        }
        
        btn = button;
        lab = label;
    }
    [view setupAutoHeightWithBottomView:vw bottomMargin:100];
}

-(void)addBgView3Subviews:(UIView *)view{
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"乐库";
    label1.font = TITLE_ONE_FONT;
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"排行榜|歌手|歌单|分类";
    label2.font = TITLE_FOUR_FONT;
    label2.textColor = TITLE_SECOND_TEXT_COLOR;
    
    UIImageView *arrowImg = [[UIImageView alloc] init];
    arrowImg.image = IMAGE(@"home_cell_arrow_left");
    
    [view sd_addSubviews:@[label1,label2,arrowImg]];
    
    label1.sd_layout.leftSpaceToView(view,20).topSpaceToView(view,20).widthIs(60).autoHeightRatio(0);
    label2.sd_layout.leftEqualToView(label1).topSpaceToView(label1,20).autoHeightRatio(0).widthIs(200);
    arrowImg.sd_layout.rightSpaceToView(view,20).centerYEqualToView(view).widthIs(10).heightEqualToWidth();
    [view setupAutoHeightWithBottomView:label2 bottomMargin:20];
}

-(void)addBgView4Subviews:(UIView *)view{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:IMAGE(@"listen_FM_highlighted") forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"音乐电台";
    label.font = TITLE_THIRD_FONT;
    label.textAlignment = NSTextAlignmentCenter;
    
    [view sd_addSubviews:@[button,label]];
    
    button.sd_layout.topSpaceToView(view,20).leftEqualToView(view).rightEqualToView(view).heightIs(40);
    label.sd_layout.topSpaceToView(button,5).leftEqualToView(button).rightEqualToView(button).autoHeightRatio(0);
    [view setupAutoHeightWithBottomView:label bottomMargin:15];
}

-(void)addBgView5Subviews:(UIView *)view{
    UIButton *button1 = [[UIButton alloc] init];
    [button1 setBackgroundImage:IMAGE(@"listen_ratio") forState:UIControlStateNormal];
    
    UIButton *button2 = [[UIButton alloc] init];
    [button2 setBackgroundImage:IMAGE(@"listen_ring_lighted") forState:UIControlStateNormal];
    
    UIButton *button3 = [[UIButton alloc] init];
    [button3 setBackgroundImage:IMAGE(@"listen_game") forState:UIControlStateNormal];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"收音机";
    label1.font = TITLE_THIRD_FONT;
    label1.textAlignment = NSTextAlignmentCenter;
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"彩铃";
    label2.font = TITLE_THIRD_FONT;
    label2.textAlignment = NSTextAlignmentCenter;
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.text = @"游戏";
    label3.font = TITLE_THIRD_FONT;
    label3.textAlignment = NSTextAlignmentCenter;
    
    [view sd_addSubviews:@[button1,button2,button3,label1,label2,label3]];
    
    button1.sd_layout.leftSpaceToView(view,20).topSpaceToView(view,15).widthIs(60).heightEqualToWidth();
    button2.sd_layout.centerXEqualToView(view).topEqualToView(button1).widthIs(button1.width).heightIs(button1.height);
    button3.sd_layout.rightSpaceToView(view,20).topEqualToView(button1).widthIs(button1.width).heightIs(button1.height);
    label1.sd_layout.leftEqualToView(button1).topSpaceToView(button1,5).widthIs(button1.width).autoHeightRatio(0);
    label2.sd_layout.leftEqualToView(button2).topEqualToView(label1).widthIs(button2.width).autoHeightRatio(0);
    label3.sd_layout.rightEqualToView(button3).topEqualToView(label1).widthIs(button3.width).autoHeightRatio(0);
    [view setupAutoHeightWithBottomView:label1 bottomMargin:10];
}

-(void)addBgView6Subviews:(UIView *)view{
    ScrollLabel *scrollLab = [[ScrollLabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-50, 40)];
    [view addSubview:scrollLab];
    [scrollLab addLabelWithText:@[@"カカカカ",@"サスケ",@"うずまきナルト"] duration:2.0];
    [view setupAutoHeightWithBottomView:scrollLab bottomMargin:0];
    
    UIImageView *arrowImg = [[UIImageView alloc] init];
    arrowImg.image = IMAGE(@"home_cell_arrow_left");
    [view addSubview:arrowImg];
    arrowImg.sd_layout.rightSpaceToView(view,20).centerYEqualToView(view).widthIs(10).heightEqualToWidth();
    
    [self setupAutoContentSizeWithBottomView:view bottomMargin:10];
}

//显示本地播放列表
-(void)showLocalList:(id)sender{
    if (self.showViewWithLocalMusic) {
        BottomPlayView *bpv = [BottomPlayView shareBottomView];
        self.showViewWithLocalMusic(_localMusicList,bpv.currentPlayIndex);
    }
}

//显示乐库
-(void)showMusicLibrary:(id)sender{
    if (self.showMusicLibrary) {
        self.showMusicLibrary();
    }
}

@end
